import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '分类编码',		name: 'dis_code' },
			{ label: '分类名称',		name: 'dis_name' },
		],
		// 提交表单列表
		formData : [
			{ label: '分类代码',		name: 'dis_code', 	half:true, required:true,  },
			{ label: '分类名称',		name: 'dis_name', 	half:true, required:true },
			{ label: '描述',		name: 'dis_desc', 	full:true, type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.fetch()
	}
	// paging
	fetch = () => $fn.fetch.call(this,'dictionary/index')
	
	// 表单
	cols = [
		{ title: '序号',			dataIndex: 'id', 			...$fn.table.num },
		{ title: '分类编码', 	dataIndex: 'dis_code', 		width:150 },
		{ title: '分类名称', 	dataIndex: 'dis_name', 		width:150 },
		{ title: '分类描述', 	dataIndex: 'dis_desc', 		width:150 },
		TableControls.controls.call(this)
	]
	
	// 表格操作按钮
	table_controls = (text,{ dis_code, id, }) => [
		TableControls.edit.call(this, 'dictionary/info', { id }, {
			idx:({ id }) => ({ id })
		}),							// 编辑
		TableControls.pushTo.call(this, '字典内容',  `data-dictionary?dis_code=${dis_code}` )
	]
	
	// 操作按钮
	controls = () => [
		Controls.add.call(this),									// 添加
		Controls.imports.call(this, 'dictionary/import' ),			// 导入
		// 下载导入模板
		Controls.importTemplate.call(this),
	]
	
	render(){
		return (
			<>
				<Page title='字典分类' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{idStr:'id'}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'dictionary/add', editApi:'dictionary/edit', onComplete:({ param })=>{
					$fn.dataRemove('dictionary/items-' + param['dis_code'] )
					$fn.dataRemove('dictionary/select')
				} }) }
			</>
		)
	}
}

export default Index